package de.duester.genealogy.adapter;

import java.util.Hashtable;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import de.duester.genealogy.R;
import de.duester.genealogy.SelectPersonActivity;
import de.duester.genealogy.SelectPlaceActivity;
import de.duester.genealogy.Utility;
import de.duester.genealogy.model.Date;
import de.duester.genealogy.model.Event;
import de.duester.genealogy.model.EventAttribute;
import de.duester.genealogy.model.Person;
import de.duester.genealogy.model.PersonRoleCode;
import de.duester.genealogy.model.Place;
import de.duester.genealogy.model.RoleCode;
import de.duester.genealogy.model.dao.PersonEventDao.Properties;
import de.duester.genealogy.model.service.EventService;

public class EventAttributeListAdapter extends ArrayAdapter<EventAttribute> {
	private Activity activity;
	private Person person;
	private Event event;
	private List<EventAttribute> eventAttributes;
	private Hashtable<Integer, Object> mixedEvents;

	public EventAttributeListAdapter(Context context, int textViewResourceId, Person person,
			Event event, List<EventAttribute> eventAttributes) {
		super(context, textViewResourceId, eventAttributes);
		activity = (Activity) context;
		this.person = person;
		this.event = event;
		this.eventAttributes = eventAttributes;
		mixedEvents = new Hashtable<Integer, Object>(eventAttributes.size());
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v;

		if (convertView == null) {
			LayoutInflater li = activity.getLayoutInflater();
			v = li.inflate(R.layout.event_attribute, null);
		} else
			v = convertView;

		TextView tvRole = (TextView) v.findViewById(R.id.tvRole);
		ImageView ivIcon = (ImageView) v.findViewById(R.id.ivIcon);

		final TextView tvValue = (TextView) v.findViewById(R.id.tvValue);

		Button btnEdit = (Button) v.findViewById(R.id.btnEdit);
		btnEdit.setBackgroundResource(R.drawable.btn_edit);
		btnEdit.setEnabled(true);

		Button btnClear = (Button) v.findViewById(R.id.btnClear);
		btnClear.setBackgroundResource(R.drawable.btn_clear);
		btnClear.setEnabled(true);
		btnClear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tvValue.setText("");
			}
		});

		final RoleCode role = EventService.getRole(eventAttributes.get(position).getRoleCode());
		tvRole.setText(role.getRole());

		switch (eventAttributes.get(position).getAttributeType()) {
		case EventAttribute.PERSON:
			ivIcon.setImageResource(R.drawable.person);
			// TODO mehrere Personen mit gleicher Rolle behandeln!
			Person person = EventService.getPerson(event, eventAttributes.get(position)
					.getRoleCode());
			if (person != null) {
				tvValue.setText(person.toString());
				if (mixedEvents.get(position) == null)
					mixedEvents.put(
							position,
							Utility.getDaoSession()
									.getPersonEventDao()
									.queryBuilder()
									.where(Properties.PersonId.eq(person.getId()),
											Properties.EventId.eq(event.getId())).list().get(0));
			}
			if (this.person.equals(person)) {
				btnEdit.setEnabled(false);
				btnClear.setEnabled(false);
			}

			btnEdit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(activity, SelectPersonActivity.class);
					intent.putExtra("personId", EventAttributeListAdapter.this.person.getId());
					intent.putExtra("position", position);
					intent.putExtra("gender", ((PersonRoleCode) role).getGender().intValue());
					activity.startActivityForResult(intent, EventAttribute.PERSON);
				}
			});
			break;
		case EventAttribute.PLACE:
			ivIcon.setImageResource(R.drawable.place);
			// TODO mehrere Orte mit gleicher Rolle behandeln?
			final Place place = EventService.getPlace(event, eventAttributes.get(position)
					.getRoleCode());
			if (place != null) {
				tvValue.setText(place.toString());
				if (mixedEvents.get(position) == null)
					mixedEvents
							.put(position,
									Utility.getDaoSession()
											.getEventPlaceDao()
											.queryBuilder()
											.where(de.duester.genealogy.model.dao.EventPlaceDao.Properties.EventId
													.eq(event.getId()),
													de.duester.genealogy.model.dao.EventPlaceDao.Properties.PlaceId
															.eq(place.getId())).list().get(0));
			}

			btnEdit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(activity, SelectPlaceActivity.class);
					intent.putExtra("position", position);
					activity.startActivityForResult(intent, EventAttribute.PLACE);
				}
			});
			break;
		case EventAttribute.DATE:
			ivIcon.setImageResource(R.drawable.date);
			Date date = EventService.getDate(event, eventAttributes.get(position).getRoleCode());
			if (date != null)
				tvValue.setText(date.toString());
			break;
		case EventAttribute.STRING:
			ivIcon.setImageResource(R.drawable.string);
			String s = EventService.getString(event, eventAttributes.get(position).getRoleCode());
			if (s != null)
				tvValue.setText(s);
			break;
		default:
			break;
		}

		return v;
	}

	@Override
	public EventAttribute getItem(int position) {
		return eventAttributes.get(position);
	}

	@Override
	public int getCount() {
		return eventAttributes.size();
	}

	public Object getMixedEvent(int position) {
		return mixedEvents.get(position);
	}

	public void setMixedEvent(int position, Object mixedEvent) {
		mixedEvents.put(position, mixedEvent);
	}

}
